﻿@{
    ViewData["Title"] = "EventManage";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <div class="layui-row">
        <div class="layui-col-xs3">
            <div class="grid-demo grid-demo-bg1">
                <div style="float:left;margin-left:20px" id="ID-tree-demo-showLine">
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="grid-demo">
                <div class="layui-tab" lay-filter="test-handle" lay-allowclose="true">
                </div>
                <div>
                    <table>
                        <tr>
                            <td>
                                <div class="layui-form layui-row">
                                    <label class="layui-form-label" style="width:100px">类型编号:</label>
                                    <div class="layui-col-md3" style="width:200px">
                                        <input type="text" name="DocumentNumber" id="DocumentNumber" placeholder="请输入" class="layui-input">
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="layui-form layui-row">
                                    <label class="layui-form-label">类型名称:</label>
                                    <div class="layui-col-md3" style="width:200px">
                                        <input type="text" name="DocumentName" id="DocumentName" placeholder="请输入" class="layui-input">
                                    </div>
                                </div>
                            </td>
                            <td style="margin-left:100px">
                                <button type="button" class="layui-btn layui-btn-primary" id="searchBtn" style="background-color:#538EBB;color:aliceblue" onclick="ShowInfo()"><i class="layui-icon"></i> 搜 索</button>
                                <button type="button" class="layui-btn layui-btn-primary" style="background-color:#EE9800"><i class="layui-icon"></i>重置</button>
                                <button type="button" class="layui-btn layui-btn-primary" lay-on="test-page" id="onAddinfo" style="background-color:green;color:black">添加</button>
                            </td>
                        </tr>
                    </table>
                    <table class="layui-hide" id="test" lay-filter="test"></table>
                    <!--添加的弹出层-->
                    <div id="add-main" style="display:none">
                        <form class="layui-form" action="" style="align-content:center" onsubmit="return false">
                            <div class="layui-form-item">
                                <label class="layui-form-label">父节点类型</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <select id="TypeInfo" name="TypeInfo" lay-filter="aihao">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">类型编码</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <input type="text" id="TypeId" name="TypeId" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">类型名称</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <input type="text" id="TypeName" name="TypeName" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">描述</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <input type="text" id="EventDesc" name="EventDesc" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">图标</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                                        <i class="layui-icon layui-icon-upload"></i> 单图片上传
                                    </button>
                                    <div class="layui-upload-list">
                                        <img class="layui-upload-img" id="Image" name="Image" style="width: 100%; height: 92px;">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">事件级别</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <select id="EventLevel" name="EventLevel" lay-filter="aihao">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">处置时限</label>
                                <div class="layui-input-inline layui-input-wrap">
                                    <select id="EventTimeLimit" name="EventTimeLimit" lay-filter="aihao">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-btn-container" style="text-align:center">
                                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">保存</button>
                            </div>
                        </form>
                    </div>
                </div>
                <script type="text/html" id="barDemo">
                    <div class="layui-clear-space">
                            <a style="color:dodgerblue;margin-right:15px"  lay-event="edit">编辑</a>
                            <a style="color:dodgerblue" lay-event="delete">删除</a>&emsp;
                    </div>
                </script>
            </div>
        </div>
    </div>
    @*     <script src="~/js/event/EventAdd.js?n=" + new Date()"></script>
    <script src="~/js/event/EventList.js?n=" +new Date()"></script> *@
    <script>

        layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table', 'tree', 'upload'], function () {
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var util = layui.util;
            var tid = null;
            var fid = null;
            var $ = layui.jquery;
            var tree = layui.tree;
            var upload = layui.upload;
            $(function () {
                ShowInfo();
                setTimeout(1);
                SelTime();
                SelLevel();
                clickTree();
            });
            // 提交事件
            form.on('submit(demo1)', function (data) {
                var obj = {};
                var field = data.field;
                obj.PId = field.TypeInfo;
                obj.TypeId = field.TypeId;
                obj.TypeName = field.TypeName;
                obj.EventDesc = field.EventDesc;
                obj.EventImage = field.Image;
                obj.EventLevel = field.EventLevel;
                obj.EventTimeLimit = field.EventTimeLimit;
                $.ajax({
                    url: "http://8.140.245.229:8000/webroute/api/Event/HandleAddEvent",
                    type: "post",
                    async:false,
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    data: JSON.stringify(obj),
                    contentType: "application/json",
                    success: res => {
                        if (res > 0) {
                            layer.confirm('添加成功', function (index) {
                                ShowInfo(1);
                                AddLogInfo("事件管理添加");
                                layer.close(index);
                                layer.close(res);
                            });

                        } else {
                            layer.alert("添加失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
            //第一个参数为操作内容，例如：登录了系统，修改/删除/添加/查询了日志表（或其他表，每次调用根据进行的操作写）
            //第二个参数为修改专用，将修改前的参数转为json字符串格式传入
            //第三个参数是将添加或删除的数据转为json传入，若是修改方法则把修改后的数据转为json字符串传入
            function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
                const data = {
                    olContext: OperationalContext,
                    PreOperation: PreOperation,
                    AfterOperation: AfterOperation
                };
                // alert(JSON.stringify(data));
                $.ajax({
                    url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
                    type: 'post',
                    dataType: 'json',
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    contentType: 'application/json', // 添加正确的 Content-Type 头部
                    data: JSON.stringify(data),
                    success: function (res) {
                        // alert(res)
                        //getListData(1);
                        console.log(res)
                    }
                })
            }
            function checkTree() {
                $("#TypeInfo").val(tid);
                layui.form.render();
            }
            function clickTree() {
                $.ajax({
                    url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventSelect?PId=" + tid,
                    async: false,
                    type: "get",
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (res) {
                        var select = $("#TypeInfo");
                        $.each(res, function (index, item) {
                            select.append($('<option>').val(item.EventTypeName).text(item.EventTypeName));
                        })
                        layui.form.render('select');
                        //设置对应 id 的节点选中
                    }
                })
            }
            function SelLevel() {
                $.ajax({
                    url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleLevel",
                    async: false,
                    type: "get",
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (res) {
                        var select = $("#EventLevel");
                        $.each(res, function (index, item) {
                            select.append($('<option>').val(item.LevelName).text(item.LevelName))
                        })
                        layui.form.render('select');
                    }
                })
            }
            function SelTime() {
                $.ajax({
                    url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleTimeLimit",
                    async: false,
                    type: "get",
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (res) {
                        var select = $("#EventTimeLimit");
                        $.each(res, function (index, item) {
                            select.append($('<option>').val(item.LimitName).text(item.LimitName))
                        })
                        layui.form.render('select');
                    }
                })
            }
            upload.render({
                elem: '#ID-upload-demo-btn',
                headers: {
                    'Authorization': "Bearer " + localStorage["token"] + ""
                },
                url: 'http://8.140.245.229:8000/webroute/api/Event/UpFile', // 实际使用时改成您自己的上传接口即可。
                done: function (res) {

                    $('#Image').attr('src', 'http://8.140.245.229:8000/webroute/File/' + res);
                    console.log($('#Image'));
                },
            });
            $('#onAddinfo').on('click', function () {
                // 批量事件
                layer.open({
                    type: 1, // page 层类型
                    area: ['1200px', '800px'],
                    title: '新增事件类型',
                    shade: 0,//控制遮罩
                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content: $("#add-main"),
                });
            });
            // 单删除
            function deleteData(id) {
                $.ajax({
                    url: `http://8.140.245.229:8000/webroute/api/Event/HandleDelEvent?Id=${id}`,
                    type: 'delete',
                    async: false,
                    success: function (res) {
                        if (res.data != 0) {
                            layer.msg('删除成功');
                            ShowInfo(1);
                            AddLogInfo("删除事件数据");
                        } else {
                            layer.msg('删除失败');
                        }
                    },
                    error: function () {
                        layer.msg('删除请求失败');
                    }
                });
            }
            // 监听工具条，实现删除功能
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event == 'delete') {
                    layer.confirm('确定删除吗？', function (index) {
                        deleteData(data.EventId); // 传递正确的数据项ID
                        layer.close(index);
                    });
                } else if (obj.event == 'edit') {
                    location.href = `/Event/EventEdit?id=${data.EventId}`;
                }
            });
            //渲染
            laydate.render({
                elem: '#storageDate'
            });
            function ShowInfo() {
                $.ajax({
                    url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleShowAdmin",
                    type: 'get',
                    dataType: 'json',
                    data: {
                        TypeId: $("#DocumentNumber").val(),
                        TypeName: $("#DocumentName").val()
                    },
                    async: false,
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (res) {
                        AddLogInfo("查询了事件管理");
                        table.render({
                            elem: '#test',
                            cols: [[
                                { type: 'checkbox', align: 'center' },
                                { field: 'TypeId', title: '类型编号', align: 'center' },
                                { field: 'PId', title: '父类型', align: 'center' },
                                { field: 'TypeName', title: '类型名称', align: 'center' },
                                { title: '操作', width: 150, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true }
                            ]],
                            data: res
                        })
                        $.ajax({
                            url: 'http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventTree',
                            data: {
                                PId: 0,
                            },
                            async: false,
                            type: 'get',
                            headers: {
                                'Authorization': "Bearer " + localStorage["token"] + ""
                            },
                            success: function (data) {
                                tree.render({
                                    //id: 'testInfo',·
                                    elem: '#ID-tree-demo-showLine',  // 容器id
                                    data: data,  // 后台返回的树形数据
                                    showLine: false, // 是否开启连接线
                                    click: function (obj) {
                                        var data = obj.data;
                                        tid = data.id;
                                        //clickTree();
                                        //checkTree();
                                    }
                                });
                            }
                        });
                    }
                })
            }
            $("#searchBtn").on('click', function () {
                ShowInfo();
            })
            function RestEmployee() {
                $(".layui-form").reset();
                layui.form.render();
            }

        });
    </script>
</body>

</html>


